package edu.sctu.yueding.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import edu.sctu.yueding.R;
import edu.sctu.yueding.bean.MySignInBean;
import edu.sctu.yueding.bean.MyTeamMemberBean;

/**
 * Created by 鑫爷您慢点 on 2016/8/2.
 */
public class MyTeamListViewAdapter extends BaseAdapter{
    private Activity activity;
    private ViewHolder viewHolder;
    List<MyTeamMemberBean> data;

    public MyTeamListViewAdapter(Activity activity,List<MyTeamMemberBean> data) {
        this.data = data;
        this.activity = activity;
    }

    /**
     * 刷新数据
     *
     * @param data
     */
    public void fresh(List<MyTeamMemberBean> data,boolean isAddAll) {

        if (isAddAll){
            this.data.addAll(data);
        }else {
            this.data = data;
        }

        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        if (data != null && data.size() > 0) {

            return data.size();

        } else {

            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        if (convertView == null) {
            view = LayoutInflater.from(activity).inflate(R.layout.item_my_team_member, null);
            viewHolder = new ViewHolder();

            viewHolder.usernameText = (TextView) view.findViewById(R.id.id_my_team_member_username);
            viewHolder.sexText = (TextView) view.findViewById(R.id.id_my_team_member_sex);
            viewHolder.IDText = (TextView) view.findViewById(R.id.id_my_team_member_ID);
            viewHolder.phoneText = (TextView) view.findViewById(R.id.id_my_team_member_phone);

            view.setTag(viewHolder);//将viewHolder储存在view中
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }

        if (data != null && !data.toString().equals("[]")){

            viewHolder.usernameText.setText(data.get(position).getRealName());
            viewHolder.phoneText.setText(data.get(position).getPhoneNumber());
            viewHolder.IDText.setText(data.get(position).getIdNumber());
            viewHolder.sexText.setText(data.get(position).getGender());
        }

        return view;
    }

    private class ViewHolder {

        TextView usernameText,IDText,phoneText,sexText;
    }
}
